﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using FonShare.Classes;
using FonShare.Classes.Data;
using FonShare.Classes.FTP;
using System.Drawing;

namespace FonShare
{
    public partial class ConfigureServer : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                int userId = (int) Session[FS_Constants.Session.USERID];
                FS_Server currentServer = FS_DataManager.GetUserServer(userId);
                if (currentServer != null)
                {
                    ServerNameTextBox.Text = currentServer.ServerName;
                    IPTextBox.Text = currentServer.IP;
                    PathTextBox.Text = currentServer.Path;
                    LoginTextBox.Text = currentServer.Login;
                    PasswordTextBox.Text = currentServer.Password;
                }
                MessageLabel.Text = string.Empty;
                TestResultLabel.Text = string.Empty;
            }
        }

        protected void ValidateButton_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(ServerNameTextBox.Text)
                || string.IsNullOrEmpty(IPTextBox.Text)
                || string.IsNullOrEmpty(LoginTextBox.Text)
                || string.IsNullOrEmpty(PasswordTextBox.Text))
            {
                MessageLabel.Text = "Content can not be empty, please fill all fields.";
            }
            else
            {
                int userId = (int)Session[FS_Constants.Session.USERID];
                FS_DataClassesDataContext dc = new FS_DataClassesDataContext();
                List<FS_Server> serversList = dc.FS_Servers.Where(record => record.OwnerID == userId).ToList();
                FS_Server currentServer = null;
                if (serversList.Count == 1)
                {
                    currentServer = serversList.First();
                    currentServer.ServerName = ServerNameTextBox.Text;
                    currentServer.IP = IPTextBox.Text;
                    currentServer.Path = string.IsNullOrEmpty(PathTextBox.Text)?string.Empty:PathTextBox.Text;
                    currentServer.Login = LoginTextBox.Text;
                    currentServer.Password = PasswordTextBox.Text;
                    dc.SubmitChanges();
                    MessageLabel.Text = "Server info updated.";
                }
                else
                {
                    FS_Server newServer = new FS_Server();
                    newServer.ServerName = ServerNameTextBox.Text;
                    newServer.IP = IPTextBox.Text;
                    newServer.Path = PathTextBox.Text;
                    newServer.Login = LoginTextBox.Text;
                    newServer.Password = PasswordTextBox.Text;
                    newServer.OwnerID = userId;
                    dc.FS_Servers.InsertOnSubmit(newServer);
                    dc.SubmitChanges();
                    MessageLabel.Text = "Server created.";
                }
            }
        }

        protected void TestServerButton_Click(object sender, EventArgs e)
        {
            TestResultLabel.Text = string.Empty;
            int userId = (int)Session[FS_Constants.Session.USERID];
            FS_Server currentServer = FS_DataManager.GetUserServer(userId);
            if (FS_FTPManager.TestFTPServer(currentServer))
            {
                TestResultLabel.Text = "Connection successful !";
                TestResultLabel.ForeColor = Color.Green;
            }
            else
            {
                TestResultLabel.Text = "Connection failed !";
                TestResultLabel.ForeColor = Color.Red;
            }
        }
    }
}
